//入口文件

global.baseName = __dirname
//全局配置文件
global.config = require(baseName + '/config/config.js')

const express = require('express')
const bodyParser = require('body-parser')
const md5 = require('md5')

//导入数据库连接
global.sequelize = require(baseName + '/db/connect.js')
//导入数据模型
global.Model = require(baseName + '/db/model/model.js')

const app = express()

//解析post请求参数
app.use(bodyParser.urlencoded({
    extended: false
}))
app.use(bodyParser.json())

//设置CORS
let allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://localhost:10003');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header('Access-Control-Allow-Credentials', 'true');
    next();
};
app.use(allowCrossDomain);

//导入路由层
let routes = require(baseName + '/routes/routes.js')
routes(app)

//监听端口
app.listen(config.serverOptions.port, (req, res) => {
    console.log(`the server running at ${config.serverOptions.host}:${config.serverOptions.port} 端口`);
})